<?php

/**
*
* @package DND Charlist
* @version $Id: functions_dnd.php$
* @copyright (c) 2010 Stephan "MOTU" König
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/


/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

/**
 *  Anfang Klasse für Chars auflisten
 */

class dndList
{
	private $id = 0;
	private $chars = array();
	private $char_classes = array();
	private $parms = array();
	private $ovlevel = 0;
	private $attributes = array();
	private $skills = array();
	
	public function dndList()
	{
		//nothing todo here?
	}
	
	public function getList($id,$parms)
	{
		if($id == 0)
		{
			if($parms != NULL && $this->testOrder($parms['order_by']))
			{
				$this->parms = $parms;
			}
			else
			{
				trigger_error('Wrong Order-Mode!');
			}
			$this->fetchChars();
		}
		else if($id > 0)
		{
			$this->id = $id;
			$this->fetchCharByID();
		}
		else
		{
			trigger_error('Wrong Char-ID!');
		}
	}
	
	private function fetchChars()
	{
		// Alle Chars aus table dnd_chars laden
		global $config, $db, $phpEx;
		
		$sql_array = array(
			'SELECT'	=> 'c.*, r.name as race, g.name as grp',
		
			'FROM'		=> array(
				DND_CHAR		=> 'c'
			),
		
			'LEFT_JOIN'	=> array(
				array(
					'FROM'	=> array(DND_RACE => 'r'),
					'ON'	=> 'r.id = c.race_id'
				),
				array(
					'FROM'	=> array(DND_GROUP => 'g'),
					'ON'	=> 'g.id = c.group_id'
				)
			),
		);
		if($this->parms['desc'] == 1)
		{
			$sql_array['ORDER_BY']	= $this->parms['order_by'] . ' DESC';
		}
		else
		{
			$sql_array['ORDER_BY']	= $this->parms['order_by'] . ' ASC';
		}
		
		$sql = $db->sql_build_query('SELECT', $sql_array);
		
		$result = $db->sql_query($sql);
		while($char_data = $db->sql_fetchrow($result)){
			$this->chars[$this->i] = $char_data;
			$this->chars[$this->i]['char_link'] = append_sid($phpbb_root_path.'dnd_char.'.$phpEx, array('CHAR_ID' => $this->chars[$this->i]['id']));
			$_tmp = new CharClasses($this->chars[$this->i]['id']);
			$this->chars[$this->i]['char_classes'] = $_tmp->getCharClasses();
			
			//echo 'char-link: ' . $this->chars[$this->i]['char_link'] . "<br>";
			$this->i++;
		}
		$db->sql_freeresult($result);
		
	}
	
	private function fetchCharByID()
	{
		// Alle Chars aus table dnd_chars laden
		global $config, $db, $phpEx;
		
		$sql_array = array(
			'SELECT'	=> 'c.*',
		
			'FROM'		=> array(
				DND_CHAR		=> 'c'
			),
		
// 			'LEFT_JOIN'	=> array(
// 				array(
// 					'FROM'	=> array(DND_RACE => 'r'),
// 					'ON'	=> 'r.id = c.race_id'
// 				),
// 				array(
// 					'FROM'	=> array(DND_GROUP => 'g'),
// 					'ON'	=> 'g.id = c.group_id'
// 				)
// 			),
			'WHERE' => 'c.id = ' . $this->id
		);
		$sql = $db->sql_build_query('SELECT', $sql_array);
		$result = $db->sql_query($sql);
		$this->chars = $db->sql_fetchrow($result);
		
		$_tmp = new Small($this->chars['alignment_id'], 'alignment');
		$this->chars['alignment'] = $_tmp->getSmall();
		
		$_tmp = new Small($this->chars['deity_id'], 'deity');
		$this->chars['deity'] = $_tmp->getSmall();
		
		$_tmp = new Small($this->chars['group_id'], 'group');
		$this->chars['group'] = $_tmp->getSmall();
		
		$_tmp = new Small($this->chars['race_id'], 'race');
		$this->chars['race'] = $_tmp->getSmall();
		
		$_tmp = new Small($this->chars['size_id'], 'size');
		$this->chars['size'] = $_tmp->getSmall();
			
		$_tmp = new CharAttributes($this->id);
		$this->chars['char_attributes'] = $_tmp->getCharAttributes();
		
		$_tmp = new CharClasses($this->id);
		$this->chars['char_classes'] = $_tmp->getCharClasses();
		
		$_tmp = new CharFeats($this->id);
		$this->chars['char_feats'] = $_tmp->getCharFeats();
		
		$_tmp = new CharSkills($this->id);
		$this->chars['char_skills'] = $_tmp->getCharSkills();

		$db->sql_freeresult($result);
		
		$this->attributes = $this->getAttributes();
		$this->skills = $this->getSkills();
		
		print_r($this->attributes);
		print_r($this->skills);
		
	}
	
	private function testOrder($order)
	{
		$orders = array ('name','race','grp');
		
		foreach($orders as $_tmp)
		{
			
			if($_tmp == $order)
			{
				return true;
			}
		}
		return false;
	}
	
	private function getAttributes()
	{
		$feat_bonus = array();
		$i = 0;
		$new_Attributes = array(	'Strength'		=> array('value' => $this->chars['char_attributes'][0]['value'],'info' => ''),
									'Dexterity'		=> array('value' => $this->chars['char_attributes'][0]['value'],'info' => ''),
									'Constitution'	=> array('value' => $this->chars['char_attributes'][0]['value'],'info' => ''),
									'Intelligence'	=> array('value' => $this->chars['char_attributes'][0]['value'],'info' => ''),
									'Wisdom'		=> array('value' => $this->chars['char_attributes'][0]['value'],'info' => ''),
									'Charisma'		=> array('value' => $this->chars['char_attributes'][0]['value'],'info' => '')
		);
		
		foreach($this->chars['char_feats'] as $_tmp)
		{
			if($_tmp['bonus']['feat_id'] > 0)
			$feat_bonus[$i] = $_tmp['bonus'];
			$i++;
		}
		$i=0;
		
		if($feat_bonus != null)
		{
			foreach($this->chars['char_attributes'] as $_tmp)
			{
				foreach($feat_bonus as $_bonus)
				{
					if($_tmp['name'] == $_bonus['attribute_name'])
					{
						$new_Attributes[$_tmp['name']]['value'] += $_bonus['value'];
						$new_Attributes[$_tmp['name']]['alt_text'] .= "{$_bonus['feat_name']}({$_bonus['value']})\n";
					}
				}
			}
		}
		
		/**
		 * TODO weapon und armor-bonie...
		 */		 		
		return $new_Attributes;
	}
	
	private function getSkills()
	{
		$skill_bonus = array();
		$i = 0;
		$_skills = new Skill(0);
		$_char_skills = new CharSkills($this->id);
		$new_Skills = $_char_skills->getCharSkills();
		$tmp = null;
		
		foreach($this->chars['char_feats'] as $_tmp)
		{
			if($_tmp['bonus']['skill_id'] > 0)
			$skill_bonus[$i] = $_tmp['bonus'];
			$i++;
		}
		$i=0;
		if($skill_bonus != null)
		{
			foreach($this->chars['char_skills'] as $_tmp)
			{
				
				foreach($skill_bonus as $_bonus)
				{
// 					var_dump($_tmp['skill_name'],);
					if($_tmp['skill_name'] == $_bonus['skill_name'])
					{
						$new_Skills[$i]['value_bonus'] += $_bonus['value'];
						$new_Skills[$i]['alt_text'] .= "{$_bonus['feat_name']}({$_bonus['value']})\n";
					}
				}
				$i++;
			}
		}
		
		/**
		 * TODO weapon und armor-bonie...
		 */		 		
		return $new_Skills;
	}
	
	public function freeCharClasses()
	{
		$this->char_classes = array();
	}
	
	public function getOverallLevel($char_id)
	{
		$this->ovlevel = 0;
		global $config, $db, $phpEx;
		
		$sql_array = array(
			'SELECT'	=> 'cc.*, c.name',
		
			'FROM'		=> array(
				DND_CHAR_CLASSES		=> 'cc'
			),
		
			'LEFT_JOIN'	=> array(
				array(
				
					'FROM'	=> array(DND_CLASS => 'c'),
					'ON'	=> 'c.id = cc.class_id'
				),
			),
		
			'WHERE'	=> 'cc.char_id = ' . $char_id
		);
		
		$sql = $db->sql_build_query('SELECT', $sql_array);
		
		$result = $db->sql_query($sql);
		while($data = $db->sql_fetchrow($result)){
			$this->ovlevel += intval($data['level']);
		}
		$db->sql_freeresult($result);
		
		return $this->ovlevel;
	}
	
	public function getChars()
	{
		return $this->chars;
	}
	
// 	public function getCharClasses($id)
// 	{
// 		$this->fetchCharClasses($id);
// 		return $this->char_classes;
// 	}
}
/**
 * Ende Klasse für Chars auflisten
 */
 
/**
 *  Anfang Klasse für EDIT-Umgebung
 */
class dndEdit
{
	private $configure = array();
	private $chars = array();
	private $classes = array();
	private $class_level_features = array();
	private $class_specials = array();
	private $char_classes = array();
	private $i = 0;
	private $ovlevel = 0;
	private $alignments = array();
	private $armor_proficiencies = array();
	private $alignment = array();
	private $tmp = array();
	

	
	public function dndEdit()
	{
		// Configuration auslesen
		global $config, $db, $phpEx;
		
		$sql_array = array(
			'SELECT'	=> 'c.*',
		
			'FROM'		=> array(
				DND_CONFIGURE		=> 'c'
			),
			
			'WHERE'	=> 'c.id = 1'
		);
		$sql = $db->sql_build_query('SELECT', $sql_array);
		$result = $db->sql_query($sql);
		$this->configure = $db->sql_fetchrow($result);
	}
	
	public function getSmall($intype)
	{
		$this->tmp = new Small(0,$intype);
		//$tmp->fetchSmall();
		//$this->tmp = $tmp->small;
		return $this->tmp;
	}
	
	public function getSmallByID($id,$intype)
	{
		$this->tmp = new Small($id, $intype);
	}
	
	public function getSkills($id)
	{
		$this->tmp = new Skill($id);
		return $this->tmp;
	}
	
	public function getSkillByID($id)
	{
		$this->tmp = new Skill($id);
	}
	
	public function getSpell()
	{
		$this->tmp = new Spell(0);
	}
	
	public function getSpellByID($id)
	{
		$this->tmp = new Spell($id);
	}
	
	public function getClasses($id)
	{
		$this->classes = new Classes($id);
		return $this->classes;
	}
	
	public function getClassLevelFeatures($id,$class_id)
	{
		$this->class_level_features = new ClassLevelFeatures($id,$class_id);
		return $this->class_level_features;
	}
	
	public function getClassSpecials($id)
	{
		$this->class_specials = new ClassSpecials($id);
		return $this->class_specials;
	}
	
	public function getClassArmorP($class_id)
	{
		$this->tmp = new ClassArmorP($class_id);
	}
	
	public function getClassWeaponP()
	{
		$this->tmp = new WeaponP();
	}
	
	
	public function getWeaponP()
	{
		$this->tmp = new WeaponP();
	}
	
	public function getTmp()
	{
		return $this->tmp;
	}
	
	public function freeTmp()
	{
		$this->tmp = array();
	}
	
	public function getChars()
	{
		return $this->chars;
	}
	
	public function getOvLevel()
	{
		return $this->ovlevel;
	}
	
	public function getConfigure()
	{
		return $this->configure;
	}
}
/**
 * Ende Klasse für EDIT-Umgebung
 */

/**
 * Funktionen für Klasse für Charls auflisten
 */
class CharClasses
{
	private $id = 0;
	private $char_classes = array();
	private $i = 0;
	
	function CharClasses($id)
	{
		if($id > 0)
		{
			$this->id = $id;
			$this->fetchCharClasses();
// 			$this->getCharClasses();
		}
		else
		{
			trigger_error('Wrong ID for CharClasses!');
		}
	}

	private function fetchCharClasses()
	{
		global $config, $db, $phpEx;
		
		$sql_array = array(
			'SELECT'	=> 'cc.*, c.name',
		
			'FROM'		=> array(
				DND_CHAR_CLASSES		=> 'cc'
			),
		
			'LEFT_JOIN'	=> array(
				array(
					'FROM'	=> array(DND_CLASS => 'c'),
					'ON'	=> 'c.id = cc.class_id'
				),
			),
		
			'WHERE'	=> 'cc.char_id = ' . $this->id
		);
		
		$sql = $db->sql_build_query('SELECT', $sql_array);
		
		$result = $db->sql_query($sql);
		while($data = $db->sql_fetchrow($result)){
			$this->char_classes[$this->i] = $data;
			$this->i++;
		}
		$db->sql_freeresult($result);
	}
	
	public function getCharClasses()
	{
		return $this->char_classes;
	}

}

class CharAttributes
{
	private $id = 0;
	private $char_attributes = array();
	private $i = 0;
	
	function CharAttributes($id)
	{
		if($id > 0)
		{
			$this->id = $id;
			$this->fetchCharAttributes();
// 			$this->getCharClasses();
		}
		else
		{
			trigger_error('Wrong ID for CharAttributes!');
		}
	}

	private function fetchCharAttributes()
	{
		global $config, $db, $phpEx;
		
		$sql_array = array(
			'SELECT'	=> 'ca.*, a.name',
		
			'FROM'		=> array(
				DND_CHAR_ATTRIBUTES		=> 'ca'
			),
		
			'LEFT_JOIN'	=> array(
				array(
					'FROM'	=> array(DND_ATTRIBUTE => 'a'),
					'ON'	=> 'a.id = ca.attribute_id'
				),
			),
		
			'WHERE'	=> 'ca.char_id = ' . $this->id
		);
		
		$sql = $db->sql_build_query('SELECT', $sql_array);
		
		$result = $db->sql_query($sql);
		while($data = $db->sql_fetchrow($result)){
			$this->char_attributes[$this->i] = $data;
			$this->i++;
		}
		$db->sql_freeresult($result);
	}
	
	public function getCharAttributes()
	{
		return $this->char_attributes;
	}

}

class CharFeats
{
	private $id = 0;
	private $char_feats = array();
	private $i = 0;
	
	function CharFeats($id)
	{
		if($id > 0)
		{
			$this->id = $id;
			$this->fetchCharFeats();
// 			$this->getCharClasses();
		}
		else
		{
			trigger_error('Wrong ID for CharFeats!');
		}
	}

	private function fetchCharFeats()
	{
		global $config, $db, $phpEx;
		
		$sql_array = array(
			'SELECT'	=> 'cf.*, f.name',
		
			'FROM'		=> array(
				DND_CHAR_FEATS		=> 'cf'
			),
		
			'LEFT_JOIN'	=> array(
				array(
					'FROM'	=> array(DND_FEAT => 'f'),
					'ON'	=> 'f.id = cf.feat_id'
				),
			),
		
			'WHERE'	=> 'cf.char_id = ' . $this->id
		);
		
		$sql = $db->sql_build_query('SELECT', $sql_array);
		
		$result = $db->sql_query($sql);
		while($data = $db->sql_fetchrow($result)){
			$this->char_feats[$this->i] = $data;
			$_tmp = new CharFeatBonus($this->char_feats[$this->i]['feat_id']);
			$this->char_feats[$this->i]['bonus'] = $_tmp->getCharFeatBonus();
			$this->i++;
		}
		$db->sql_freeresult($result);
	}
	
	public function getCharFeats()
	{
		return $this->char_feats;
	}

}

class CharFeatBonus
{
	private $id = 0;
	private $char_feat_bonus = array();
	private $i = 0;
	
	function CharFeatBonus($id)
	{
		if($id > 0)
		{
			$this->id = $id;
			$this->fetchCharFeatBonus();
// 			$this->getCharClasses();
		}
		else
		{
			trigger_error('Wrong ID for CharFeatsBonus!');
		}
	}

	private function fetchCharFeatBonus()
	{
		global $config, $db, $phpEx;
		
		$sql_array = array(
			'SELECT'	=> 'fb.*, f.name as feat_name, a.name as attribute_name, s.name as skill_name',
		
			'FROM'		=> array(
				DND_FEAT_BONUS		=> 'fb'
			),
		
			'LEFT_JOIN'	=> array(
				array(
					'FROM'	=> array(DND_FEAT => 'f'),
					'ON'	=> 'f.id = fb.feat_id'
				),
				array(
					'FROM'	=> array(DND_ATTRIBUTE => 'a'),
					'ON'	=> 'a.id = fb.attribute_id'
				),
				array(
					'FROM'	=> array(DND_SKILL => 's'),
					'ON'	=> 's.id = fb.skill_id'
				),
			),
			
			'WHERE'	=> 'fb.feat_id = ' . $this->id
		);
		
		
		$sql = $db->sql_build_query('SELECT', $sql_array);
		
		$result = $db->sql_query($sql);
		$this->char_feat_bonus = $db->sql_fetchrow($result);
		$db->sql_freeresult($result);
	}
	
	public function getCharFeatBonus()
	{
		return $this->char_feat_bonus;
	}

}

class CharSkills
{
	private $id = 0;
	private $char_skills = array();
	private $i = 0;
	private $j = 0;
	function CharSkills($id)
	{
		if($id > 0)
		{
			$this->id = $id;
			$this->fetchCharSkills();
// 			$this->getCharClasses();
		}
		else
		{
			trigger_error('Wrong ID for CharSkills!');
		}
	}

	private function fetchCharSkills()
	{
		global $config, $db, $phpEx;
		
		$sql_array = array(
			'SELECT'	=> 'cs.*, s.name as skill_name',
			
			'FROM'		=> array(
				DND_CHAR_SKILLS		=> 'cs'
			),
			
			'LEFT_JOIN'	=> array(
				array(
					'FROM'	=> array(DND_SKILL => 's'),
					'ON'	=> 's.id = cs.skill_id'
				),
			),
			
			'WHERE'	=> 'cs.char_id = ' . $this->id
		);
		
		$sql = $db->sql_build_query('SELECT', $sql_array);
// 		print_r($sql);
		$result = $db->sql_query($sql);
		while($data = $db->sql_fetchrow($result)){
			$this->char_skills[$this->i] = $data;
			$_tmp = new Skill($this->char_skills[$this->i]['skill_id']);
			$this->char_skills[$this->i]['skill_info'] = $_tmp->getSkills();
			$this->i++;
		}
		$db->sql_freeresult($result);
	}
	public function getCharSkills()
	{
		return $this->char_skills;
	}
}
 /**
 * Funktionen für Klasse für EDIT-Umgebung
 */

/**
 * Klasse für kleine Tabellen
 */
class Small
{
	private $small = array();
	private $id;
	private $table;
	private $type;
	private $typeid;
	
	private $types = array(	'alignment' ,
							'armor_proficiencies', 
							'attribute', 
							'class_special', 
							'deity', 
							'feat', 
							'group', 
							'race', 
							'size', 
							'weapon_proficiencies');
	private $i = 0;
	private $tables = array(	'alignment' => DND_ALIGNMENT,
								'armor_proficiencies' => DND_ARMOR_PROFICIENCIES, 
								'attribute' => DND_ATTRIBUTE, 
								'class_special' => DND_CLASS_SPECIAL, 
								'deity' => DND_DEITY, 
								'feat' => DND_FEAT, 
								'group' => DND_GROUP, 
								'race' => DND_RACE, 
								'size' => DND_SIZE, 
								'weapon_proficiencies' => DND_WEAPON_PROFICIENCIES);
	
	function Small($id, $type)
	{
		// Constructor für Aligment, wenn ID>0, dann direkt ID auslesen, 
		// ansonsten leeres Object
		if(isset($type))
		{
			// Testen ob $type zulässig
			$this->typeid = $this->testType($type);
			if($this->typeid >= 0) $this->type = $type;
			else trigger_error('Wrong Type for Object Small!');
		}
		else trigger_error('To few arguments for Object Small!');
		$this->id = $id;
		if($this->id > 0)	$this->fetchSmallById();
		if($this->id == 0) $this->fetchSmall();
		
	}
	
	public function fetchSmall()
	{
		//Bestimmtes ID vom Typ auslesen
		global $config, $db, $phpEx;
		
		$sql_array = array('SELECT'	=> 'a.*', 
							'FROM'=> array( $this->tables[$this->type]	=> 'a'));
		$sql = $db->sql_build_query('SELECT', $sql_array);
		$result = $db->sql_query($sql);
		while($_small = $db->sql_fetchrow($result)){
			$this->small[$this->i] = $_small;
			if($this->type == 'group') $this->small[$this->i]['extra'] = $_small['dungeon_master'];
			if($this->type == 'feat') $this->small[$this->i]['extra'] = $_small['special_only'];
			if($this->type == 'size') $this->small[$this->i]['extra'] = $_small['size_mod'];
			$this->small[$this->i]['edit_link'] = append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('mode' => 'edit', 'intype' => $this->type, 'inid' => $_small['id']));
			$this->small[$this->i]['delete_link'] = append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('mode' => 'delete', 'intype' => $this->type, 'inid' => $_small['id']));
			$this->i++;
		}
	}
	
	public function fetchSmallByID()
	{
		//Bestimmtes ID vom Typ auslesen
		global $config, $db, $phpEx;
		
		$tmp = array ($this->table	=> 'a');
		$sql_array = array('SELECT'	=> 'a.*', 
							'FROM'=> array( $this->tables[$this->type]	=> 'a'),
							'WHERE' => 'a.id = ' . $this->id);
		$sql = $db->sql_build_query('SELECT', $sql_array);
		$result = $db->sql_query($sql);
		$this->small = $db->sql_fetchrow($result);
		if($this->type == 'group') $this->small['extra'] = $this->small['dungeon_master'];
		if($this->type == 'feat') $this->small['extra'] = $this->small['special_only'];
		if($this->type == 'size') $this->small['extra'] = $this->small['size_mod'];
	}
	
	public function setSmall($id, $name, $info, $intype, $extra, $mode)
	{
		global $config, $db, $phpEx;
		
		if($extra !== NULL)
		{
			switch($intype)
			{
				case 'feat': 
						$tmp = 'special_only';
				break;
				case 'group': 
						$tmp = 'dungeon_master';
				break;
				case 'size': 
						$tmp = 'size_mod';
				break;
				case 'alignment': 
						$tmp = 'short';
				break;
				default:
						$tmp = '';
				break;
			}
			$data = array(
							'name'			=> (string) $name,
							'info'			=> (string) $info,
							$tmp	=> $extra
					);
		}
		else 
		{
			$data = array(
							'name'			=> (string) $name,
							'info'			=> (string) $info,
					);
		}
		
		if($mode == 'UPDATE')
		{
			$sql = 'UPDATE ' . $this->tables[$this->type] . ' SET ' . $db->sql_build_array('UPDATE', $data) . ' WHERE id = ' . (int) $id;
			$db->sql_query($sql);
		}
		else if($mode == 'INSERT')
		{
			$sql = 'INSERT INTO ' . $this->tables[$this->type] . ' ' . $db->sql_build_array('INSERT', $data);
			$db->sql_query($sql);
		}
		else
		{
			trigger_error('WRONG SQL-MODE!');
		}
	}
	
	public function deleteSmall()
	{
		global $config, $db, $phpEx;
		$sql_in = array($this->id);
		$sql = 'DELETE FROM ' . $this->tables[$this->type] . ' WHERE ' . $db->sql_in_set('id', $sql_in);
		$db->sql_query($sql);
	}
	
	private function testType($intype)
	{
		$i=0;
		
		foreach($this->types as $type)
		{
			if($intype == $type){
				return $i;
			}
			$i++;
		}
		
		return -1;
	}
	
	public function getSmall()
	{
		return $this->small;
	}
	
	public function getID()
	{
		return $this->id;
	}
	
	public function getTable()
	{
		return $this->Table;
	}
}

class Spell
{
	private $id;
	private $spell = array();
	private $spells = array();
	private $i = 0;
	
	public function Spell($id)
	{
		if($id > 0)
		{
			$this->id = $id;
			$this->fetchSpellByID();
		}
		else if($id == 0)
		{
			$this->fetchSpells();
		}
	}
	
	public function fetchSpells()
	{
		//Bestimmtes Alignment auslesen
		global $config, $db, $phpEx;
		$sql_array = array(
			'SELECT'	=> 'a.*',
			'FROM'		=> array(
				DND_SPELLS		=> 'a'
			)
		);
		$sql = $db->sql_build_query('SELECT', $sql_array);
		$result = $db->sql_query($sql);
		while($_spell = $db->sql_fetchrow($result))
		{
			$this->spells[$this->i] = $_spell;
			$this->spells[$this->i]['edit_link'] = append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('mode' => 'edit', 'intype' => 'spell', 'inid' => $_spell['id']));
			$this->spells[$this->i]['delete_link'] = append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('mode' => 'delete', 'intype' => 'spell', 'inid' => $_spell['id']));
			$this->i++;
		}
		
	}
	
	public function fetchSpellByID()
	{
		//Bestimmtes Alignment auslesen
		global $config, $db, $phpEx;
		$sql_array = array(
			'SELECT'	=> 'a.*',
			'FROM'		=> array(
				DND_SPELLS		=> 'a'
			),
			'WHERE' => 'a.id = ' . $this->id
		);
		$sql = $db->sql_build_query('SELECT', $sql_array);
		$result = $db->sql_query($sql);
		$this->spells = $db->sql_fetchrow($result);
	}
	
	public function setSpell(
		$id,
		$name,
		$school,
		$level,
		$components,
		$casting_time,
		$range,
		$effect,
		$duration,
		$saving_throw,
		$spell_resistance,
		$info,
		$mode)
	{
		global $config, $db, $phpEx;
		
		$data = array(
						'`name`'				=> (String) $name,
						'`school`'				=> (String) $school,
						'`level`'				=> $level,
						'`components`'			=> (String) $components,
						'`casting_time`'		=> (String) $casting_time,
						'`range`'				=> (String) $range,
						'`effect`'				=> (String) $effect,
						'`duration`'			=> (String) $duration,
						'`saving_throw`'		=> (String) $saving_throw,
						'`spell_resistance`'	=> (String) $spell_resistance,
						'`info`'				=> $info,
						);
		
		if($mode == 'UPDATE')
		{
			$sql = 'UPDATE ' . DND_SPELLS . ' SET ' . $db->sql_build_array('UPDATE', $data) . ' WHERE id = ' . (int) $id;
			$db->sql_query($sql);
		}
		else if($mode == 'INSERT')
		{
			$sql = 'INSERT INTO ' . DND_SPELLS . ' ' . $db->sql_build_array('INSERT', $data);
			$db->sql_query($sql);
		}
		else
		{
			trigger_error('WRONG SQL-MODE!');
		}
	}
	
	public function deleteSpell()
	{
		global $config, $db, $phpEx;
		$sql_in = array($this->id);
		$sql = 'DELETE FROM ' . DND_SPELLS . ' WHERE ' . $db->sql_in_set('id', $sql_in);
		$db->sql_query($sql);
	}
	
	public function getSpell()
	{
		return $this->spell;
	}
	
	public function getSpells()
	{
		return $this->spells;
	}
}

class Classes
{
	private $id = 0;
	private $intype;
	private $i = 0;
	private $j = 0;
	private $classes = array();
	private $classlevels = array();
	
	public function Classes($id)
	{
		if($id > 0)
		{
			$this->id = $id;
			$this->fetchClassByID();
		}
		else if($id == 0)
		{
			$this->fetchClasses();
		}
	}
	
	public function fetchClasses()
	{
		//Bestimmtes Alignment auslesen
				
		global $config, $db, $phpEx;
		$sql_array = array(
			'SELECT'	=> 'c.*, a.name as cast_attribute_name',
			'FROM'		=> array(
				DND_CLASS		=> 'c'
			),
			'LEFT_JOIN' => array(
				array(
					'FROM'	=> array(DND_ATTRIBUTE => 'a'),
					'ON'	=> 'a.id = c.cast_attribute_id'
				)
			),
		);
		$sql = $db->sql_build_query('SELECT', $sql_array);
		$result = $db->sql_query($sql);
		//var_dump($result);
		while($_class = $db->sql_fetchrow($result))
		{
			//print_r($_class);
			$this->classes[$this->i] = $_class;
			$this->classes[$this->i]['edit_link'] = append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('mode' => 'edit', 'intype' => 'classe', 'inid' => $_class['id']));
			$this->classes[$this->i]['delete_link'] = append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('mode' => 'delete', 'intype' => 'classe', 'inid' => $_class['id']));
			//$this->classes[$this->i]['skills'] = new ClassSkills($this->id);
			/*
			*	Get all Armor_Proficiencies
			*/		
			$_sql_array = array(
				'SELECT'	=> 'cap.*,ap.name as ap_name',
				'FROM'		=> array(
					DND_CLASS_ARMOR_P		=> 'cap'
				),
				'LEFT_JOIN' => array(
					array(
						'FROM'	=> array(DND_ARMOR_PROFICIENCIES => 'ap'),
						'ON'	=> 'ap.id = cap.armor_p_id'
					)
				),
				'WHERE' => 'cap.class_id = ' . $_class['id'],
				'ORDER_BY' => 'ap.name ASC'
			);
			$_sql = $db->sql_build_query('SELECT', $_sql_array);
			$_result = $db->sql_query($_sql);
			$this->j = 0;
			while($_armor_p = $db->sql_fetchrow($_result))
			{
				$this->classes[$this->i][armor_p][$this->j] = $_armor_p;
				$this->j++;
			}
			/*
			*	Get all Weapon_Proficiencies
			*/
			$_sql_array = array(
				'SELECT'	=> 'cwp.*,wp.name as wp_name',
				'FROM'		=> array(
					DND_CLASS_WEAPON_P		=> 'cwp'
				),
				'LEFT_JOIN' => array(
					array(
						'FROM'	=> array(DND_WEAPON_PROFICIENCIES => 'wp'),
						'ON'	=> 'wp.id = cwp.weapon_p_id'
					)
				),
				'WHERE' => 'cwp.class_id = ' . $_class['id'],
				'ORDER_BY' => 'wp.name ASC'
			);
			$_sql = $db->sql_build_query('SELECT', $_sql_array);
			$_result = $db->sql_query($_sql);
			$this->j = 0;
			while($_weapon_p = $db->sql_fetchrow($_result))
			{
				$this->classes[$this->i][weapon_p][$this->j] = $_weapon_p;
				$this->j++;
				
			}
			
			/*
			*	Get all Class_Alignments
			*/
			$_sql_array = array(
				'SELECT'	=> 'ca.*,a.name as a_name',
				'FROM'		=> array(
					DND_CLASS_ALIGNMENTS		=> 'ca'
				),
				'LEFT_JOIN' => array(
					array(
						'FROM'	=> array(DND_ALIGNMENT => 'a'),
						'ON'	=> 'a.id = ca.alignment_id'
					)
				),
				'WHERE' => 'ca.class_id = ' . $_class['id'],
				'ORDER_BY' => 'a.name ASC'
			);
			$_sql = $db->sql_build_query('SELECT', $_sql_array);
			$_result = $db->sql_query($_sql);
			$this->j = 0;
			while($_alignment = $db->sql_fetchrow($_result))
			{
				$this->classes[$this->i][alignments][$this->j] = $_alignment;
				$this->j++;
				
			}
			
			/*
			*	Get all Class_Skills
			*/
			$_sql_array = array(
				'SELECT'	=> 'cs.*,s.name as s_name',
				'FROM'		=> array(
					DND_CLASS_SKILLS		=> 'cs'
				),
				'LEFT_JOIN' => array(
					array(
						'FROM'	=> array(DND_SKILL => 's'),
						'ON'	=> 's.id = cs.skill_id'
					)
				),
				'WHERE' => 'cs.class_id = ' . $_class['id'],
				'ORDER_BY' => 's.name ASC'
			);
			$_sql = $db->sql_build_query('SELECT', $_sql_array);
			$_result = $db->sql_query($_sql);
			$this->j = 0;
			while($_alignment = $db->sql_fetchrow($_result))
			{
				$this->classes[$this->i][skills][$this->j] = $_alignment;
				$this->j++;
				
			}
			//$this->classes[$this->i]['armor_proficiencies'] = new small($this->id, 'armor_proficiencies');
			//$this->classes[$this->i]['weapon_proficiencies'] = new small($this->id, 'weapon_proficiencies');
			//$this->classes[$this->i]['class_level_features'] = new ClassLevelFeatures($this->id);
			$this->i++;
		}
		//print_r($this->classes);
		
	}
	
	public function fetchClassByID()
	{
		//Bestimmtes Alignment auslesen
		
		global $config, $db, $phpEx;
		$sql_array = array(
			'SELECT'	=> 'c.*,a.name as cast_attribute_name',
			'FROM'		=> array(
				DND_CLASS		=> 'c'
			),
			'LEFT_JOIN' => array(
				array(
					'FROM'	=> array(DND_ATTRIBUTE => 'a'),
					'ON'	=> 'a.id = c.cast_attribute_id'
				)
			),
			'WHERE' => 'c.id = ' . $this->id
		);
		$sql = $db->sql_build_query('SELECT', $sql_array);
		$result = $db->sql_query($sql);
		$this->classes = $db->sql_fetchrow($result);
		//print_r($this->classes);
		/*
		*	Get all Armor_Proficiencies
		*/		
		$sql_array = array(
			'SELECT'	=> 'cap.*,ap.name as ap_name',
			'FROM'		=> array(
				DND_CLASS_ARMOR_P		=> 'cap'
			),
			'LEFT_JOIN' => array(
				array(
					'FROM'	=> array(DND_ARMOR_PROFICIENCIES => 'ap'),
					'ON'	=> 'ap.id = cap.armor_p_id'
				)
			),
			'WHERE' => 'cap.class_id = ' . $this->id,
		);
		$sql = $db->sql_build_query('SELECT', $sql_array);
		$result = $db->sql_query($sql);
		$this->i = 0;
		$this->classes[armor_p] = array();
		while($_armor_p = $db->sql_fetchrow($result))
		{
			$this->classes[armor_p][$this->i] = $_armor_p;
			$this->i++;
		}
		/*
		*	Get all Weapon_Proficiencies
		*/
		$sql_array = array(
			'SELECT'	=> 'cwp.*,wp.name as wp_name',
			'FROM'		=> array(
				DND_CLASS_WEAPON_P		=> 'cwp'
			),
			'LEFT_JOIN' => array(
				array(
					'FROM'	=> array(DND_WEAPON_PROFICIENCIES => 'wp'),
					'ON'	=> 'wp.id = cwp.weapon_p_id'
				)
			),
			'WHERE' => 'cwp.class_id = ' . $this->id
		);
		$sql = $db->sql_build_query('SELECT', $sql_array);
		$result = $db->sql_query($sql);
		$this->i = 0;
		$this->classes[weapon_p] = array();
		while($_weapon_p = $db->sql_fetchrow($result))
		{
			$this->classes[weapon_p][$this->i] = $_weapon_p;
			$this->i++;
		}
				/*
		*	Get all Alignments
		*/
		$sql_array = array(
			'SELECT'	=> 'ca.*,a.name as a_name',
			'FROM'		=> array(
				DND_CLASS_ALIGNMENTS		=> 'ca'
			),
			'LEFT_JOIN' => array(
				array(
					'FROM'	=> array(DND_ALIGNMENT => 'a'),
					'ON'	=> 'a.id = ca.alignment_id'
				)
			),
			'WHERE' => 'ca.class_id = ' . $this->id
		);
		$sql = $db->sql_build_query('SELECT', $sql_array);
		$result = $db->sql_query($sql);
		$this->i = 0;
		$this->classes[alignments] = array();
		while($_alignment = $db->sql_fetchrow($result))
		{
			$this->classes[alignments][$this->i] = $_alignment;
			$this->i++;
		}
		/*
		*	Get all Skills
		*/
		$sql_array = array(
			'SELECT'	=> 'cs.*,s.name as s_name',
			'FROM'		=> array(
				DND_CLASS_SKILLS		=> 'cs'
			),
			'LEFT_JOIN' => array(
				array(
					'FROM'	=> array(DND_SKILL => 's'),
					'ON'	=> 's.id = cs.skill_id'
				)
			),
			'WHERE' => 'cs.class_id = ' . $this->id
		);
		$sql = $db->sql_build_query('SELECT', $sql_array);
		$result = $db->sql_query($sql);
		$this->i = 0;
		$this->classes[skills] = array();
		while($_skills = $db->sql_fetchrow($result))
		{
			$this->classes[skills][$this->i] = $_skills;
			$this->i++;
		}		
		
		//print_r($this->classes);
	}
	
	public function setClass(
		$id,
		$name,
		$hit_dice,
		$cast_attribute_id,
		$armor_p = array(),
		$weapon_p = array(),
		$alignments = array(),
		$skills = array(),
		$info,
		$mode)
	{
		global $config, $db, $phpEx;
		$sql_in = array($this->id);
		$this->i = 0;
		
		//var_dump("id: ",$id);
		//print_r($armor_p);
		
		$data = array(
						'`name`'					=> (String) $name,
						'`hit_dice`'				=> (String) $hit_dice,
						'`cast_attribute_id`'		=> $cast_attribute_id,
						'`info`'					=> $info,
						);
		
		if($mode == 'UPDATE')
		{
			
			$sql = 'UPDATE ' . DND_CLASS . ' SET ' . $db->sql_build_array('UPDATE', $data) . ' WHERE id = ' . (int) $id;
			$db->sql_query($sql);
			
			// Alle Weapon- und Armor-Proficiencies löschen und neue auswahl einfügen
			
			$sql = 'DELETE FROM ' . DND_CLASS_ARMOR_P . ' WHERE ' . $db->sql_in_set('class_id', $sql_in);
			$db->sql_query($sql);
			
			$sql = 'DELETE FROM ' . DND_CLASS_WEAPON_P . ' WHERE ' . $db->sql_in_set('class_id', $sql_in);
			$db->sql_query($sql);
			
			$sql = 'DELETE FROM ' . DND_CLASS_ALIGNMENTS . ' WHERE ' . $db->sql_in_set('class_id', $sql_in);
			$db->sql_query($sql);
			
			$sql = 'DELETE FROM ' . DND_CLASS_SKILLS . ' WHERE ' . $db->sql_in_set('class_id', $sql_in);
			$db->sql_query($sql);
			
			
			foreach($armor_p as $_data)
			{
				$data_ap[$this->i] = array(
							'`class_id`' => $_data[class_id],
							'`armor_p_id`' => $_data[armor_p_id],
						);
				$this->i++;
			}
			$db->sql_multi_insert(DND_CLASS_ARMOR_P, $data_ap);
			$this->i = 0;
			foreach($weapon_p as $_data)
			{
				$data_wp[$this->i] = array(
							'`class_id`' => $_data[class_id],
							'`weapon_p_id`' => $_data[weapon_p_id],
						);
				$this->i++;
				
			}
			$db->sql_multi_insert(DND_CLASS_WEAPON_P, $data_wp);
			$this->i = 0;
			foreach($alignments as $_data)
			{
				$data_al[$this->i] = array(
							'`class_id`' => $_data[class_id],
							'`alignment_id`' => $_data[alignment_id],
						);
				$this->i++;
				
			}
			$db->sql_multi_insert(DND_CLASS_ALIGNMENTS, $data_al);
			$this->i = 0;
			foreach($skills as $_data)
			{
				$data_sk[$this->i] = array(
							'`class_id`' => $_data[class_id],
							'`skill_id`' => $_data[skill_id],
						);
				$this->i++;
				
			}
			$db->sql_multi_insert(DND_CLASS_SKILLS, $data_sk);
		}
		else if($mode == 'INSERT')
		{
			$this->i = 0;
			$data_ap = array();
			$data_wp = array();
			
			$sql = 'INSERT INTO ' . DND_CLASS . ' ' . $db->sql_build_array('INSERT', $data);
			$db->sql_query($sql);
			
			$sql_array = array(
				'SELECT'	=> 'c.*',
				'FROM'		=> array(
					DND_CLASS		=> 'c'
				),
				'WHERE' => 'c.name = ' . '\'' . $name . '\''
			);
			$sql = $db->sql_build_query('SELECT', $sql_array);
			$result = $db->sql_query($sql);
			$tmp = $db->sql_fetchrow($result);
			$id = $tmp['id'];
			
			//var_dump("ID: ",$id);
			
			foreach($armor_p as $_data)
			{
				$data_ap[$this->i] = array(
							'`class_id`' => $id,
							'`armor_p_id`' => $_data[armor_p_id],
						);
				$this->i++;
			}
			
			$this->i = 0;
			foreach($weapon_p as $_data)
			{
				$data_wp[$this->i] = array(
							'`class_id`' => $id,
							'`weapon_p_id`' => $_data[weapon_p_id],
						);
				$this->i++;
			}
			$this->i = 0;
			foreach($alignments as $_data)
			{
				$data_al[$this->i] = array(
							'`class_id`' => $id,
							'`alignment_id`' => $_data[alignment_id],
						);
				$this->i++;
				
			}
			$this->i = 0;
			foreach($skills as $_data)
			{
				$data_sk[$this->i] = array(
							'`class_id`' => $id,
							'`skill_id`' => $_data[skill_id],
						);
				$this->i++;
				
			}
			
			$db->sql_multi_insert(DND_CLASS_ARMOR_P, $data_ap);
			$db->sql_multi_insert(DND_CLASS_WEAPON_P, $data_wp);
			$db->sql_multi_insert(DND_CLASS_ALIGNMENTS, $data_al);
			$db->sql_multi_insert(DND_CLASS_SKILLS, $data_sk);
		}
		else
		{
			trigger_error('WRONG SQL-MODE!');
		}
	}
	
	public function deleteClass()
	{
		global $config, $db, $phpEx;
		$sql_in = array($this->id);
		
		$sql = 'DELETE FROM ' . DND_CLASS_ARMOR_P . ' WHERE ' . $db->sql_in_set('class_id', $sql_in);
		$db->sql_query($sql);
		
		$sql = 'DELETE FROM ' . DND_CLASS_WEAPON_P . ' WHERE ' . $db->sql_in_set('class_id', $sql_in);
		$db->sql_query($sql);
		
		$sql = 'DELETE FROM ' . DND_CLASS . ' WHERE ' . $db->sql_in_set('id', $sql_in);
		$db->sql_query($sql);
	}
	
	public function getClasses()
	{
		return $this->classes;
	}
}

class ClassLevelFeatures
{
	private $id = 0;
	private $class_id = 0;
	private $class_level = 0;
	private $class_level_features = array();
	private $i = 0;
	private $j = 0;
	
	public function ClassLevelFeatures($id, $class_id)
	{
		if($id > 0 && $class_id > 0)
		{
			$this->id = $id;
			$this->class_id = $class_id;
			$this->fetchClassLevelFeaturesByID();
			$this->class_level = $this->class_level_features['class_level'];
		}
		else if($id == 0 && $class_id > 0)
		{
			$this->class_id = $class_id;
			$this->fetchClassLevelFeaturesByClassID();
		}
		else if($id == 0 && $class_id == 0)
		{
			$this->fetchClassLevelFeatures();
		}
	
	}
	public function fetchClassLevelFeatures()
	{
		global $config, $db, $phpEx;
		$sql_array = array(
			'SELECT'	=> 'clf.* , c.name',
			'FROM'		=> array(
				DND_CLASS_LEVEL_FEATURES		=> 'clf',
			),
			'LEFT_JOIN' => array(
				array(
					'FROM'	=> array(DND_CLASS	=> 'c'),
					'ON'	=> 'c.id = clf.class_id'
				),
			),
			//'WHERE' => 'c.class_id = ' . $this->class_id
		);
		$sql = $db->sql_build_query('SELECT', $sql_array);
		$result = $db->sql_query($sql);
		while($_data = $db->sql_fetchrow($result))
		{
// 			print_r($_data);
			$this->class_level_features[$this->i] = $_data;
			$this->class_level_features[$this->i]['edit_link'] = append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('mode' => 'edit', 'intype' => 'clf', 'inid' => $_data['id'], 'class_id' => $_data['class_id']));
			$this->class_level_features[$this->i]['delete_link'] = append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('mode' => 'delete', 'intype' => 'clf', 'inid' => $_data['id'], 'class_id' => $_data['class_id']));
			$this->class_level_features[$this->i]['class_link'] = append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('intype' => 'clf', 'class_id' => $_data['class_id']));
			
			$specials = new ClassSpecials(0,$this->class_level_features[$this->i]['class_id'],$this->class_level_features[$this->i]['class_level']);
			
			$specials = $specials->getClassSpecials();
// 			print_r($specials);
			
			for($this->j = 0; 	$this->j < 4; 	$this->j++)
			{
				$s_id = 'special_' . ($this->j + 1);
				if($specials[$this->j] != NULL)
				{
					$this->class_level_features[$this->i][$s_id] = $specials[$this->j];
				}
				else
				{
					$this->class_level_features[$this->i][$s_id] = 0;
				}
			}
			
			$this->i++;
		}
		$this->i = 0;
	}
	
	public function fetchClassLevelFeaturesByClassID()
	{
		global $config, $db, $phpEx;
		$sql_array = array(
			'SELECT'	=> 'clf.* , c.name',
			'FROM'		=> array(
				DND_CLASS_LEVEL_FEATURES		=> 'clf',
			),
			'LEFT_JOIN' => array(
				array(
					'FROM'	=> array(DND_CLASS	=> 'c'),
					'ON'	=> 'c.id = clf.class_id'
				),
			),
			'WHERE' => 'clf.class_id = ' . $this->class_id
		);
		$sql = $db->sql_build_query('SELECT', $sql_array);
		$result = $db->sql_query($sql);
		while($_data = $db->sql_fetchrow($result))
		{
			$this->class_level_features[$this->i] = $_data;
			$this->class_level_features[$this->i]['edit_link'] = append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('mode' => 'edit', 'intype' => 'clf', 'inid' => $_data['id'], 'class_id' => $_data['class_id']));
			$this->class_level_features[$this->i]['delete_link'] = append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('mode' => 'delete', 'intype' => 'clf', 'inid' => $_data['id']));
			
			$specials = new ClassSpecials(0,$this->class_level_features[$this->i]['class_id'],$this->class_level_features[$this->i]['class_level']);
			$specials = $specials->getClassSpecials();
			
			for($this->j = 0; 	$this->j < 4; 	$this->j++)
			{
				$s_id = 'special_' . ($this->j + 1);
				if($specials[$this->j] != NULL)
				{
					$this->class_level_features[$this->i][$s_id] = $specials[$this->j];
				}
				else
				{
					$this->class_level_features[$this->i][$s_id] = 0;
				}
			}
			
			$this->i++;
		}
		$specials = array();
		$this->i = 0;
		
	}
	
	public function fetchClassLevelFeaturesByID()
	{
// 		echo "HIER!12<br>";
		
		global $config, $db, $phpEx;
		$sql_array = array(
			'SELECT'	=> 'clf.* , c.name',
			'FROM'		=> array(
				DND_CLASS_LEVEL_FEATURES		=> 'clf',
			),
			'LEFT_JOIN' => array(
				array(
					'FROM'	=> array(DND_CLASS	=> 'c'),
					'ON'	=> 'c.id = clf.class_id'
				),
			),
			'WHERE' => 'clf.class_id = ' . $this->class_id . ' AND clf.id = ' . $this->id
		);
		$sql = $db->sql_build_query('SELECT', $sql_array);
		$result = $db->sql_query($sql);
		$this->class_level_features = $db->sql_fetchrow($result);
		//$specials = array();
		
		
		$specials = new ClassSpecials(0,$this->class_level_features['class_id'],$this->class_level_features['class_level']);
		$specials = $specials->getClassSpecials();

		
		for($this->j = 0; 	$this->j < 4; 	$this->j++)
		{
			$s_id = 'special_' . ($this->j + 1);
			if($specials[$this->j] != NULL)
			{
				$this->class_level_features[$s_id] = $specials[$this->j];
			}
			else
			{
				$this->class_level_features[$s_id] = 0;
			}
		}
	}
	
	public function setClassLevelFeatures(	$id,
											$class_id,
											$class_level,
											$base_attack_bonus_1,
											$base_attack_bonus_2,
											$base_attack_bonus_3,
											$base_save_fort,
											$base_save_ref,
											$base_save_will,
											$specials,
											$spells_per_day,
											$mode
											)
	{
		global $config, $db, $phpEx;
		
		$this->id = $id;
		
		$data = array(
				'class_id'				=> intval($class_id),
				'class_level'			=> intval($class_level),
				'base_attack_bonus_1'	=> intval($base_attack_bonus_1),
				'base_attack_bonus_2'	=> intval($base_attack_bonus_2),
				'base_attack_bonus_3'	=> intval($base_attack_bonus_3),
				'base_save_fort'		=> intval($base_save_fort),
				'base_save_ref'			=> intval($base_save_ref),
				'base_save_will'		=> intval($base_save_will),
		);
		
		for($i = 0; $i < 10; $i++)
		{
			$name = 'spells_per_day_lvl_' . $i;
			if($spells_per_day[$i] != 0)
			{
				$data[$name] = $spells_per_day[$i];
			}
			else
			{
				$data[$name] = 0;
			}
		}
		
		
		if($mode == 'UPDATE')
		{
			$sql = 'UPDATE ' . DND_CLASS_LEVEL_FEATURES . ' SET ' . $db->sql_build_array('UPDATE', $data) . ' WHERE id = ' . (int) $this->id;
			$db->sql_query($sql);
			
			/*
			 * Alle zugehörigen CLASS_SPECIALS löschen und später neu/bearbeitet einfügen
			 * 
			 */
			$sql = 'DELETE FROM ' . DND_CLASS_SPECIALS . ' WHERE `class_id` = ' . $class_id . ' AND `class_level` = ' . $class_level;//$db->sql_in_set('class_id', $sql_in);
			$db->sql_query($sql);
			
			foreach($specials as $_set)
			{
				if($_set != NULL)
				{
					$sql = 'INSERT INTO ' . DND_CLASS_SPECIALS . ' ' . $db->sql_build_array('INSERT', $_set);
					$db->sql_query($sql);
				}
			}
		}
		else if($mode == 'INSERT')
		{
			
			$sql = 'INSERT INTO ' . DND_CLASS_LEVEL_FEATURES . ' ' . $db->sql_build_array('INSERT', $data);
			$db->sql_query($sql);
			
			foreach($specials as $_set)
			{
				if($_set != NULL)
				{
// 					print_r($_set);
					$sql = 'INSERT INTO ' . DND_CLASS_SPECIALS . ' ' . $db->sql_build_array('INSERT', $_set);
					$db->sql_query($sql);
				}
			}
		}
		else
		{
			trigger_error('WRONG SQL-MODE!');
		}
	}
	
	public function deleteClassLevelFeature()
	{
		global $config, $db, $phpEx;
		
		echo "ClassID: {$this->class_id}; ClassLVL: {$this->class_level}";
		$sql_in = array($this->id);
		$sql = 'DELETE FROM ' . DND_CLASS_LEVEL_FEATURES . ' WHERE ' . $db->sql_in_set('id', $sql_in);
		$db->sql_query($sql);
		
		$sql = 'DELETE FROM ' . DND_CLASS_SPECIALS . ' WHERE `class_id` = ' . $this->class_id . ' AND `class_level` = ' . $this->class_level;
		$db->sql_query($sql);
	}
	
	public function getClassLevelFeatures()
	{
		return $this->class_level_features;
	}
}

class ClassSpecials
{
	private $id = 0;
	private $class_id = 0;
	private $class_level = 0;
	private $class_specials = array();
	private $i = 0;
	
	public function ClassSpecials($id,$class_id,$class_level)
	{
		$this->id = $id;
		if($id > 0 && $class_id == 0)
		{
			$this->fetchClassSpecialByID();
		}
		else if($id == 0 && $class_id == 0)
		{
			$this->fetchClassSpecials();
		}
		else if($id == 0 && $class_id > 0 && $class_level > 0)
		{
			$this->class_id = $class_id;
			$this->class_level = $class_level;
			$this->fetchClassSpecialsByClass();
		}
		else
		{
			$this->class_specials = NULL;
		}
	}
	
	public function fetchClassSpecials()
	{
// 		global $config, $db, $phpEx;
// 		$sql_array = array(
// 			'SELECT'	=> 'cs.*',
// 			'FROM'		=> array(
// 				DND_CLASS_SPECIALS		=> 'cs',
// 			),
// 		);
// 		$sql = $db->sql_build_query('SELECT', $sql_array);
// 		$result = $db->sql_query($sql);
// 		
// 		while($_class_special = $db->sql_fetchrow($result))
// 		{
// 			$this->class_specials[$this->i] = $_class_special;
// 			$this->i++;
// 		}
// 		$this->class_specials = $db->sql_fetchrow($result);
		trigger_error(" fetchClassSpecials() is not implemented yet!");
	}
	
	public function fetchClassSpecialByID()
	{
// 		global $config, $db, $phpEx;
// 		$sql_array = array(
// 			'SELECT'	=> 'cs.*',
// 			'FROM'		=> array(
// 				DND_CLASS_SPECIALS		=> 'cs',
// 			),
// 			'WHERE' => 'cs.id = ' . $this->id
// 			
// 		);
// 		$sql = $db->sql_build_query('SELECT', $sql_array);
// 		$result = $db->sql_query($sql);
// 		$this->class_specials = $db->sql_fetchrow($result);
		trigger_error(" fetchClassSpecialByID() is not implemented yet!");
	}
	
	public function fetchClassSpecialsByClass()
	{
		global $config, $db, $phpEx;
		$sql_array = array(
			'SELECT'	=> 'cs.*',
			'FROM'		=> array(
				DND_CLASS_SPECIALS		=> 'cs',
			),
			'WHERE' => 'cs.class_id = ' . $this->class_id . ' AND cs.class_level = ' . $this->class_level
			
		);
		$sql = $db->sql_build_query('SELECT', $sql_array);
		$result = $db->sql_query($sql);
		//$this->class_specials = $db->sql_fetchrow($result);
		
		while($_class_special = $db->sql_fetchrow($result))
		{
			$this->class_specials[$this->i] = $_class_special;
			if($this->class_specials[$this->i]['skill_id'] != 0)
			{
				$_tmp = new Skill($this->class_specials[$this->i]['skill_id']);
				$_tmp = $_tmp->getSkills();
				$this->class_specials[$this->i]['name'] = $_tmp['name'];
			}
			if($this->class_specials[$this->i]['feat_id'] != 0)
			{
				$_tmp = new Small($this->class_specials[$this->i]['feat_id'],'feat');
				$_tmp = $_tmp->getSmall();
				$this->class_specials[$this->i]['name'] = $_tmp['name'];
			}
			if($this->class_specials[$this->i]['class_special_id'] != 0)
			{
				$_tmp = new Small($this->class_specials[$this->i]['class_special_id'],'class_special');
				$_tmp = $_tmp->getSmall();
				$this->class_specials[$this->i]['name'] = $_tmp['name'];
			}
			$this->i++;
		}
		
	}
	
	//print_r($this->class_specials);
	
	public function getClassSpecials()
	{
		return $this->class_specials;
	}
}

class ClassSkills
{
	private $id = 0;
	private $class_skills = array();
	
	public function ClassSkills($id)
	{
		if($id > 0)
		{
			$this->id = $id;
			$this->fetchClassSkillByID();
		}
		else if($id == 0)
		{
			$this->fetchClassSkills();
		}
	}
	
	public function fetchClassSkills()
	{
		//Bestimmtes Alignment auslesen
		global $config, $db, $phpEx;
		$sql_array = array(
			'SELECT'	=> 'cs.* , s.*',
			'FROM'		=> array(
				DND_CLASS_SKILLS		=> 'cs',
			),
			'LEFT_JOIN' => array(
				array(
					'FROM'	=> array(DND_SKILL	=> 's'),
					'ON'	=> 's.id = cs.class_id'
				)
			),
		);
		$sql = $db->sql_build_query('SELECT', $sql_array);
		$result = $db->sql_query($sql);
		while($_skill = $db->sql_fetchrow($result))
		{
			$this->class_skills[$this->i] = $_skill;
			$this->i++;
		}
	}
}


class Skill
{
	private $skills = array();
	private $id;
	private $i = 0;

	
	public function Skill($id)
	{
		if($id > 0)
		{
			$this->id = $id;
			$this->fetchSkillById();
		}
		else if($id == 0)
		{
			$this->fetchSkills();
		}
		else trigger_error("Skill-ID doesn't exists!");
		
	}
	
	public function fetchSkills()
	{
		//Bestimmtes ID vom Typ auslesen
		global $config, $db, $phpEx;
		
		$sql_array = array('SELECT'	=> 's.*,a.name AS attribute_name', 
							'FROM'=> array( 
								DND_SKILL	=> 's'
							),
							'LEFT_JOIN' => array(
								array(
									'FROM' 	=> array(DND_ATTRIBUTE => 'a'),
									'ON'	=> 's.attribute_id = a.id'
								)
							),
						);
		$sql = $db->sql_build_query('SELECT', $sql_array);
		$result = $db->sql_query($sql);
		while($_skill = $db->sql_fetchrow($result)){
			$this->skills[$this->i] = $_skill;
			$this->skills[$this->i]['edit_link'] = append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('mode' => 'edit', 'intype' => 'skill', 'inid' => $_skill['id']));
			$this->skills[$this->i]['delete_link'] = append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('mode' => 'delete', 'intype' => 'skill', 'inid' => $_skill['id']));
			$this->i++;
		}
		//var_dump($this->skills);
	}
	
	public function fetchSkillByID()
	{
		//Bestimmtes ID vom Typ auslesen
		global $config, $db, $phpEx;
		
		$sql_array = array('SELECT'	=> 's.*,a.name AS attribute_name', 
							'FROM'=> array( DND_SKILL	=> 's'),
							'LEFT_JOIN' => array(
								array(
									'FROM' 	=> array(DND_ATTRIBUTE => 'a'),
									'ON'	=> 's.attribute_id = a.id'
								)
							),
							'WHERE' => 's.id = ' . $this->id);
		$sql = $db->sql_build_query('SELECT', $sql_array);
		$result = $db->sql_query($sql);
		$this->skills = $db->sql_fetchrow($result);
		//$this->getAttributes();
	}
	
	public function setSkill($id, $name, $info, $attribute_id, $use_untrained, $armor_check_penalty, $mode)
	{
		global $config, $db, $phpEx;
		
		$data = array(
				'name'					=> (string) $name,
				'info'					=> (string) $info,
				'attribute_id'			=> $attribute_id,
				'use_untrained'			=> intval($use_untrained),
				'armor_check_penalty'	=> intval($armor_check_penalty)
		);

		
		if($mode == 'UPDATE')
		{
			$sql = 'UPDATE ' . DND_SKILL . ' SET ' . $db->sql_build_array('UPDATE', $data) . ' WHERE id = ' . (int) $id;
			$db->sql_query($sql);
		}
		else if($mode == 'INSERT')
		{
			$sql = 'INSERT INTO ' . DND_SKILL . ' ' . $db->sql_build_array('INSERT', $data);
			$db->sql_query($sql);
		}
		else
		{
			trigger_error('WRONG SQL-MODE!');
		}
	}
	
	public function deleteSkill()
	{
		global $config, $db, $phpEx;
		$sql_in = array($this->id);
		$sql = 'DELETE FROM ' . DND_SKILL . ' WHERE ' . $db->sql_in_set('id', $sql_in);
		$db->sql_query($sql);
	}
	
	/*
	public function getAttributes()
	{
		global $config, $db, $phpEx;
		
		$sql_array = array('SELECT'	=> 'a.id, a.name', 
							'FROM'=> array( DND_ATTRIBUTE	=> 'a'),
		);
		$sql = $db->sql_build_query('SELECT', $sql_array);
		$result = $db->sql_query($sql);
		$this->i = 0;
		//$this->skills[attributes] = array();
		while($_attribute = $db->sql_fetchrow($result))
		{
			$this->skills[attributes][$this->i] = $_attribute;
			$this->i++;
		}
	}
	*/
	public function getSkills()
	{
		return $this->skills;
	}
}

class ClassArmorP
{
	private $armorp = array();
	private $i = 0;
	private $id;
	
	public function ClassArmorP($class_id)
	{
		global $config, $db, $phpEx;
		
		$this->id = $class_id;
		
		$sql_array = array('SELECT'	=> 'a.*', 
							'FROM'=> array( DND_CLASS_ARMOR_P	=> 'a'),
							'WHERE class_id = '. $this->id
							);
		$sql = $db->sql_build_query('SELECT', $sql_array);
		$result = $db->sql_query($sql);
		
		while($_armorp = $db->sql_fetchrow($result)){
			$this->armorp[$this->i] = $_armorp;
			$this->i++;
		}
	}
	
	public function getArmorP()
	{
		return $this->armorp;
	}
}

class WeaponP
{
	private $weaponp = array();
	private $i = 0;
	
	public function WeaponP()
	{
		global $config, $db, $phpEx;
		
		$sql_array = array('SELECT'	=> 'a.*', 
							'FROM'=> array( DND_WEAPON_PROFICIENCIES	=> 'a'));
		$sql = $db->sql_build_query('SELECT', $sql_array);
		$result = $db->sql_query($sql);
		
		while($_weaponp = $db->sql_fetchrow($result)){
			$this->weaponp[$this->i] = $_weaponp;
			$this->i++;
		}
	}
	
	public function getWeaponP()
	{
		return $this->weaponp;
	}
}

/*
class Deity
{
	var $id;
	var $deity = array();
	
	function Deity($id)
	{
		$this->id = $id;
		if($this->id > 0) fetchDeity();
	}
	
	function fetchAlignment()
	{
		//Bestimmtes Alignment auslesen
		global $config, $db, $phpEx;
		
		$sql_array = array(
			'SELECT'	=> 'a.*',
		
			'FROM'		=> array(
				DND_DEITY		=> 'a'
			),
			'WHERE' => 'a.id = ' . $this->id
		);
		$sql = $db->sql_build_query('SELECT', $sql_array);
		$result = $db->sql_query($sql);
		$this->deity = $db->sql_fetchrow($result);
	}
	
	function setDeity($id, $name, $short, $info, $mode)
	{
		global $config, $db, $phpEx;
		
		$data = array(
						'name'	=> (string) $name,
						'short'	=> (string) $short,
						'info'	=> (string) $info
				);
		if($mode == 'UPDATE')
		{
			$sql = 'UPDATE ' . DND_DEITY . ' SET ' . $db->sql_build_array('UPDATE', $data) . ' WHERE id = ' . (int) $id;
			//var_dump($inid, $sql);
			$db->sql_query($sql);
		}
		else if($mode == 'INSERT')
		{
			$sql = 'INSERT INTO ' . DND_DEITY . ' ' . $db->sql_build_array('INSERT', $data);
			//var_dump($inid, $sql);
			$db->sql_query($sql);
		}
		else
		{
			trigger_error('WRONG SQL-MODE!');
		}
	}
} */
?>